package com.miyu.system.service.openSystem.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miyu.common.dao.commonDAO.book.BookRuleSetDao;
import com.miyu.common.dao.commonDAO.cos.CosCityDao;
import com.miyu.common.pojo.WebServicePJ;
import com.miyu.common.pojo.book.BookRuleSet;
import com.miyu.common.pojo.cos.CosCity;
import com.miyu.common.utils.comService.WebHttpService;
import com.miyu.common.utils.ehcache.EhcacheUtil;
import com.miyu.system.dao.openSystem.OpenSystemDao;
import com.miyu.system.service.openSystem.OpenSystemService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 开放系统服务impl
 *
 * @author longmy
 * @date 2022/04/01
 */
@Service
public class OpenSystemServiceImpl implements OpenSystemService {

    @Resource
    private OpenSystemDao openSystemDao;
    @Resource
    private CosCityDao cosCityDao;
    @Resource
    private BookRuleSetDao bookRuleSetDao;

    /**
     * //自定触发缓存 和手动触发缓存
     * 获取城市下拉数据
     * @return
     */

    @Cacheable(cacheNames = "CosCity" ,key = "'CosCity'")
    @Override
    public List<CosCity> queryCity() {
        List<CosCity> cityList=new ArrayList<>();
        JSONArray rs = new JSONArray();
        /*由于数据频繁使用加入到缓存中*/
        if(EhcacheUtil.getInstance().hasKey("bookEhcache","city")){
            cityList = (List<CosCity>) EhcacheUtil.getInstance().get("bookEhcache", "city");
        }else{
            Sort sort = Sort.by(Sort.Direction.ASC,"id");
            cityList = cosCityDao.findAll(sort);
            EhcacheUtil.getInstance().put("bookEhcache","city",cityList);
        }
        return cityList;
    }

    /**
     * 规则集dict类型查询书
     *
     * @param param 参数
     * @return {@code List<BookRuleSet>}
     */
    @Override
    @Cacheable(cacheNames = "bookRuleSet" ,key = "#param.containsKey('ruleId')?#param.getString('ruleId'):'all'")
    public List<BookRuleSet> queryBookRuleSetDict(JSONObject param) {
        String ruleId="ruleId";
        List<BookRuleSet> bookRuleSet=new ArrayList<>();
        Sort sort = Sort.by(Sort.Direction.ASC,"ruleId");
        if (param.containsKey(ruleId)){
            List ruleIds = new ArrayList();
            ruleId=param.getString(ruleId);
            ruleIds.add(ruleId);
            if(ruleIds.size()>0) {
                bookRuleSet = bookRuleSetDao.findAllById(ruleIds);
            }
        }else {
            bookRuleSet = bookRuleSetDao.findAll(sort);
        }
        return bookRuleSet;
    }

    /**
     * 得到天气
     *
     * @param param 参数
     * @return {@link JSONObject}
     */

    @Override
    @Cacheable(cacheNames = {"weather#3600"},key = "#param.getString('vCity')")
    public JSONObject getWeather(JSONObject param) throws Exception {
        String key = param.getString("vCity");
        String version ="";
        version=param.getString("version");
        String city = param.getString("city");
        JSONObject rs = new JSONObject();
        //数据进行缓存 校验数据是否有 如果有是否超过两小时 如果超过一个小时更新 否者直接取值返回不再更新时间
        StringBuffer url = new StringBuffer();
        url.append("https://www.tianqiapi.com/api?");
        url.append("unescape=1");
        url.append("&version=").append(version);
        url.append("&appid=16319171&appsecret=Ywlwceu5");
        url.append("&cityid=").append(city);
        if(EhcacheUtil.getInstance().hasKey("weather",key)){
            rs = (JSONObject) EhcacheUtil.getInstance().get("weather",key );
        }else {
            // 返回结果字符串
            String result = null;
            WebServicePJ pj = new WebServicePJ();
            pj.setUrl(url.toString());
            pj.setMethod("GET");
            result = WebHttpService.getWebHttpServiceStr(pj);
            if(result.length()>0) {
                rs = JSONObject.parseObject(result);
            }
            EhcacheUtil.getInstance().put("weather",key ,rs,3600);
        }
        return rs;
    }


}
